package com.hmkj.admin.controller.operate;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.entity.export.ExportUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.mapper.operate.BannerMapper;
import com.hmkj.core.model.operate.BannerModel;
import com.hmkj.core.po.operate.Banner;
import com.hmkj.core.service.operate.BannerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/*
                          _.._        ,------------.
                       ,'      `.    ( hello gays,your code never bug.... )
                      /  __) __` \    `-,----------'
                     (  (`-`(-')  ) _.-'
                     /)  \  = /  (
                    /'    |--' .  \
                   (  ,---|  `-.)__`
                    )(  `-.,--'   _`-.
                   '/,'          (  fk",
                    (_       ,    `/,-' )
                    `.__,  : `-'/  /`--'
                      |     `--'  |
                      `   `-._   /
                       \        (
                       /\ .      \.
                      / |` \     ,-\
                     /  \| .)   /   \
                    ( ,'|\    ,'     :
                    | \,`.`--"/      }
                    `,'    \  |,'    /
                   / "-._   `-/      |
                   "-.   "-.,'|     ;
                  /        _/["---'""]
                 :        /  |"-     '
                 '           |      /
                             `      |
*/
@RestController
@RequestMapping(BaseController.BASE_URI)
public class BannerJsonController extends GenericController<Integer, Banner> {
    @Resource
    private BannerService bannerService;

    /**
     * 列表json数据
     *
     * @param dtGridPager
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/banner/json/list")
    @RequiresPermissions("sys:banner:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        DtGrid dtgrid = dataAuthority(dtGridPager, null);
        return bannerService.getDtGridList(dtgrid, BannerModel.class);
    }

    /**
     * 新增
     *
     * @param banner
     * @return
     */
//    @ResponseBody
    @RequestMapping("/banner/json/add")
    @RequiresPermissions("sys:banner:save")
    public ResultEntity addBanner(Banner banner) {
        checkBanner(banner);
        banner.setCreateTime(new Date());
        banner.setDelFlag(Banner.DELFLAG.NORMAL.code);
        bannerService.insert(banner);
        return ok();
    }

    /**
     * 编辑
     *
     * @param banner
     * @return
     */
    @RequestMapping("/banner/json/edit")
    @RequiresPermissions("sys:banner:edit")
    public ResultEntity editBanner(Banner banner, Integer id) {
        banner.setId(id);
        Banner persistBanner = bannerService.selectByPrimaryKey(id);
        if (null == persistBanner || persistBanner.getDelFlag() == Banner.DELFLAG.DELETED.code) {
            throw new BussinessException("banner不存在或者已被删除");
        }
        checkBanner(banner);
        banner.setDelFlag(Banner.DELFLAG.NORMAL.code);
        bannerService.updateByPrimaryKeySelective(banner);
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/banner/json/del")
    @RequiresPermissions("sys:banner:edit")
    public ResultEntity deleteBanner(Integer id) {
        Banner persistBanner = bannerService.selectByPrimaryKey(id);
        if (null == persistBanner || persistBanner.getDelFlag() == Banner.DELFLAG.DELETED.code) {
            throw new BussinessException("banner不存在或者已被删除");
        }
        bannerService.removeById(persistBanner);
        return ok();
    }

    /**
     * 检查Banner合法性
     *
     * @param banner
     */
    private void checkBanner(Banner banner) {
        if(StringUtils.isEmpty(banner.getAgentId())){
            throw new BussinessException("请选择代理商");
        }else if (StringUtils.isEmpty(banner.getTitle())) {
            throw new BussinessException("请输入标题");
        } else if (StringUtils.isEmpty(banner.getPicPath())){
            throw new BussinessException("请上传图片");
        } else if (null == banner.getSortNo()) {
            throw new BussinessException("请输入排序值");
        }
    }

    /**
     * 列表导出
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "banner/json/export", method = RequestMethod.POST)
    public ResultEntity export(String dtGridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 执行导出
        ExportUtils.export(request, response, bannerService.getDtGridListExport(dtGridPager));
        return ok();
    }
}
